﻿using ConfExpert.Forms.Base;

using DevTeam;


using System;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo
{
    public partial class StokGruplari : XForm
    {
        public StokGruplari()
        {
            InitializeComponent();
        }

        BindingSource bs, bs2;
        private void StokGruplari_Load(object sender, EventArgs e)
        {
            ds.xStokGruplariTop.Columns.Add("SgEmpty", typeof(int), "Iif(StokGrupId is null,1,0)");
            ds.xStokGruplari.Columns.Add("SgEmpty", typeof(int), "Iif(SUM(Child(FK_xStokGruplariTop_xStokGruplari_ProductId).SgEmpty)>0, SUM(Child(FK_xStokGruplariTop_xStokGruplari_ProductId).SgEmpty), null)");
            ds.xStokGruplari.Columns["StokGrupId"].Expression = "MAX(Child(FK_xStokGruplariTop_xStokGruplari_ProductId).StokGrupId)";
            //ds.xStokGruplari.Columns["DescK"].Expression = "MAX(Child(FK_xStokGruplariTop_xStokGruplari_ProductId).DescK)";
            ds.xStokGruplari.Columns.Add("MinStokGrupId", typeof(int), "MIN(Child(FK_xStokGruplariTop_xStokGruplari_ProductId).StokGrupId)");
            ds.xStokGruplari.Columns.Add("SgMulti", typeof(char), "Iif(StokGrupId = MinStokGrupId OR StokGrupId is null AND MinStokGrupId is null,null,'*')");
            ds.xStokGruplari.Columns.Add("AlisFiyati", typeof(string), "MAX(Child(FK_xStokGruplariTop_xStokGruplari_ProductId).AlisFiyati)");

            bs = new BindingSource(ds, "xStokGruplari");
            bs2 = new BindingSource(bs, "FK_xStokGruplariTop_xStokGruplari_ProductId");

            InitGridView(gridView1, bs);
            InitGridView(gridView2, bs2);
            gridView1.OptionsView.ShowGroupPanel = true;
            gridView1.MakeColumnInvisible("MinStokGrupId");

            gridView2.MakeColumnInvisible("Scan", "ProductId", "SgEmpty");

            gridView1.Columns["SgEmpty"].VisibleIndex = gridView1.Columns["SgMulti"].VisibleIndex = 0;
            gridView1.FormatAsNumber(1, "Qty");
            gridView2.FormatAsNumber(1, "Qty");
            gridView1.AddSummary("Qty");
            gridView2.AddSummary("Qty");
            AddGroupSummary(gridView1, "Qty");

            GridManager gm = GridManager.GetManager(gridView1);
            gm.GridMenu.AddMenu("Kartela etiketi bas", PrintNumuneEtiket);

            splitContainerControl1.AutoSize();
            gridView2.ShowingEditor += new System.ComponentModel.CancelEventHandler(gridView2_ShowingEditor);
            gridView1.OptionsBehavior.Editable = false;

            gridView1.FocusedRowChanged += new DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventHandler(gridView1_FocusedRowChanged);
            btnKaydet.Visibility = ytk(138) ? DevExpress.XtraBars.BarItemVisibility.Always : DevExpress.XtraBars.BarItemVisibility.Never;
            btnKaydet.ItemClick += btnKaydet_ItemClick;

            //  ds.xStokGruplariTop.StokGrupIdColumn

            Rfrsh();

            if (!ytk(140))  //Meyzi+Gamze+Betül
            {
                gridView1.MakeColumnInvisible("AlisFiyati");
                gridView2.MakeColumnInvisible("AlisFiyati");
            }
        }

        void btnKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridView1.EndEditing();
            gridView2.EndEditing();
            ExecuteNonQuery("UpdStokGruplari", ds.xStokGruplariTop.SerializeChanges());
            Rfrsh();
        }

        DateTime Tarih = new DateTime(2000, 1, 1);

        private void PrintNumuneEtiket(object sender, EventArgs e)
        {
            string productIds = GetProductIds();
            using (KartelaEtiketi ke = new KartelaEtiketi(1, productIds))
                ke.ShowDialog();
        }
        private string GetProductIds()
        {
            string ids = "";
            foreach (int rowHandle in gridView1.GetSelectedRows())
            {
                if (rowHandle > -1)
                {
                    string pId = gridView1.GetRowCellValue(rowHandle, "ProductId").ToString();

                    if (pId != "")
                        ids += pId + ",";
                }
            }

            ids = ids.RemoveIfEndsWith(",");

            return ids;
        }
        void Rfrsh()
        {
            GetDetails("xStokGruplari;xStokGruplariTop;xTarih", "GetStokGruplari", Tarih);
            ds.xStokGruplariTop.AcceptChanges();
            gridView1.BestFitColumns();
            Tarih = ds.xTarih[0].Tarih;
        }

        void gridView2_ShowingEditor(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = gridView2.FocusedColumn.FieldName != "StokGrupId" &&
                gridView2.FocusedColumn.FieldName != "DescK" &&
                gridView2.FocusedColumn.FieldName != "AlisFiyati";
        }

        void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (intParse(gridView1.GetFocusedRowCellValue("ProductId")) > 0)
                txtInfo.Caption = gridView1.GetFocusedRowCellDisplayText("ProductId");
        }
    }
}